﻿/**
 * Input
 * 
 * @version 2012.01.18
 * @author  Bruno Miguel de Oliveira Tamer <bmotamer@gmail.com>
 */

#region C# Libraries

using System.Collections.Generic;

#endregion

#region XNA Libraries

using Microsoft.Xna.Framework.Input;

#endregion

namespace main.sdk.input
{

    public static class Input
    {

        private static readonly byte[] State = new byte[256];

        private static readonly byte UNTOUCHED  = 0;
        private static readonly byte TRIGGERED  = 1;
        private static readonly byte PRESSED    = 2;
        private static readonly byte RELEASED   = 3;

        public static void Update()
        {
            KeyboardState state = Keyboard.GetState();
            for (short key = 255; key >= 0; key--)
            {
                byte keyState = State[key];
                State[key] = state.IsKeyDown((Keys)key) ?
                    ((keyState == UNTOUCHED) || (keyState == RELEASED)) ?  TRIGGERED : PRESSED   :
                   !((keyState == UNTOUCHED) || (keyState == RELEASED)) ?  RELEASED  : UNTOUCHED;
            }
        }

        public static bool Untouched(Keys key) { return State[(byte)key] == UNTOUCHED; }
        public static bool Trigger  (Keys key) { return State[(byte)key] == TRIGGERED; }
        public static bool Press    (Keys key) { return State[(byte)key] == PRESSED; }
        public static bool Release  (Keys key) { return State[(byte)key] == RELEASED; }

        public static bool Down(Keys key)
        {
            byte keyState = State[(byte)key];
            return !((keyState == UNTOUCHED) || (keyState == RELEASED));
        }

        public static bool AnyDown()
        {
            foreach (byte keyState in State)
                if (!((keyState == UNTOUCHED) || (keyState == RELEASED)))
                    return true;
            return false;
        }

        public static bool AnyTrigger()
        {
            foreach (byte keyState in State)
                if (keyState == TRIGGERED)
                    return true;
            return false;
        }

        public static bool AnyPress()
        {
            foreach (byte keyState in State)
                if (keyState == PRESSED)
                    return true;
            return false;
        }

        public static bool AnyReleased()
        {
            foreach (byte keyState in State)
                if (keyState == RELEASED)
                    return true;
            return false;
        }

    }

}